package com.zzyl.serve.service.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanAllVo;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author xiaoyingying
 * @date 2025-10-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService  {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Autowired
    private RedisTemplate  redisTemplate;

    private static final String NURSING_PLAN_KEY = "nursing_plan_";

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //先查护理计划
        NursingPlan plan = this.getById(id);
        //在查总表
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery(NursingProjectPlan.class).eq(NursingProjectPlan::getPlanId, id));
        //封装
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(plan, NursingPlanVo.class);
        List<NursingProjectPlanVo> list = nursingProjectPlans.stream().map(Project -> {
            NursingProjectPlanVo nursingProjectPlanVo = BeanUtil.toBean(Project, NursingProjectPlanVo.class);
            nursingProjectPlanVo.setProjectId(Project.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(Project.getExecuteCycle().toString());
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
    //新增护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        this.save(nursingPlan);
    //新增护理项目
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        for (NursingProjectPlan projectPlan : projectPlans) {
            projectPlan.setPlanId(nursingPlan.getId());
        }

        boolean b = nursingProjectPlanService.saveBatch(projectPlans);
        redisTemplate.delete(NURSING_PLAN_KEY);
        return b ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto dto)
    {
        //1 对象拷贝
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);

        //2.更新中间表
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if (CollUtil.isNotEmpty(projectPlans)) {
            //删除
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId,nursingPlan.getId()));
            //新增
            projectPlans.forEach(projectPlan->{
                projectPlan.setPlanId(nursingPlan.getId());
            });
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        //3.修改计划表
        boolean flag = this.updateById(nursingPlan);
        redisTemplate.delete(NURSING_PLAN_KEY);
        return flag?1:0;
    }

    /**
     * 批量删除护理计划
     *
     * @param
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanByIds(Long id)
    {
        boolean b = this.removeById(id);

        nursingProjectPlanService.remove(Wrappers.lambdaQuery(NursingProjectPlan.class).eq(NursingProjectPlan::getPlanId, id));
        redisTemplate.delete(NURSING_PLAN_KEY);

            return b ? 1 : 0 ;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        boolean b = removeById(id);
        redisTemplate.delete(NURSING_PLAN_KEY);
        return b ? 1 : 0;
    }
    /**
     * 查询所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingPlanAllVo> selectAll() {
        List<NursingPlanAllVo>list = (List<NursingPlanAllVo>) redisTemplate.opsForValue().get(NURSING_PLAN_KEY);
        if (CollUtil.isNotEmpty(list)){
            return list;
        }
        List<NursingPlanAllVo> nursingPlanAllVos = nursingPlanMapper.selectAll();
        redisTemplate.opsForValue().set(NURSING_PLAN_KEY,nursingPlanAllVos);
        return    nursingPlanAllVos;
    }
}
